package com.sun.elasticjob.config;

import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * zk注册中心配置
 */
@Configuration
public class ZkRegistryCenterConfig {

    @Value("${regCenter.serverList}")
    private String serverList;
    @Value("${regCenter.namespace}")
    private String namespace;
    @Value("${regCenter.sessionTimeoutMilliseconds}")
    private int sessionTimeoutMilliseconds;
    @Value("${regCenter.connectionTimeoutMilliseconds}")
    private int connectionTimeoutMilliseconds;
    @Value("${regCenter.maxRetries}")
    private int maxRetries;
    @Value("${regCenter.startedTimeoutMilliseconds}")
    private long startedTimeoutMilliseconds;
    @Value("${regCenter.completedTimeoutMilliseconds}")
    private long completedTimeoutMilliseconds;

    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter registryCenter() {
        ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(serverList, namespace);
        zkConfig.setSessionTimeoutMilliseconds(sessionTimeoutMilliseconds);
        zkConfig.setConnectionTimeoutMilliseconds(connectionTimeoutMilliseconds);
        zkConfig.setMaxRetries(maxRetries);
        return new ZookeeperRegistryCenter(zkConfig);
    }

    @Bean
    public ElasticJobListener elasticJobListener() {
        return new ElasticJobListener(startedTimeoutMilliseconds, completedTimeoutMilliseconds);
    }

}
